package com.xuecheng.content.api.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @Author:王亚栋
 * @Date:2023/8/13 21:51
 * @Version:1.0
 * 获取当前用户信息的工具类
 */
@Slf4j(topic = "SecurityUtils")
public class SecurityUtils {

    public static XcUser getUser(){

        try {

            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            if (principal instanceof String){

                //取出用户身份信息
                String userInfo = principal.toString();

                //将Json转为Java对象
                XcUser xcUser = JSON.parseObject(userInfo, XcUser.class);

                return xcUser;
            }

        }catch (Exception e){

            log.error("获取当前登录用户信息{}出错",e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    @Data
    public static class XcUser implements Serializable{

        private static final long serialVersionUID = 1L;

        private String id;

        private String username;

        private String password;

        private String salt;

        private String name;
        private String nickname;
        private String wxUnionid;
        private String companyId;
        /**
         * 头像
         */
        private String userpic;

        private String utype;

        private LocalDateTime birthday;

        private String sex;

        private String email;

        private String cellphone;

        private String qq;

        /**
         * 用户状态
         */
        private String status;

        private LocalDateTime createTime;

        private LocalDateTime updateTime;


    }

}
